<template>
  <div class="header">
    <div class="query">
      <!--<button @click="selectAll">全选</button>-->
      <label>
        <input type="checkbox" v-model="select" class="checkbox">
      </label>
      <input type="text" v-model="record" placeholder="请输入信息" @keyup.enter="recorded">
    </div>
  </div>
</template>

<script>
  import {mapState,mapActions} from 'vuex'
  export default {
    name: "itemHeader",
    data(){
      return {
        record: '',
      }
    },
    computed:{
      ...mapState({
        todos:(state)=>{
          return state.todos;
        }
      }),
      select:{
        get(){
          return this.todos.every(item=>item.completed === true) && this.todos.length !== 0;
        },
        set(value){
          this.selectAll(value);
        }
      }
    },
    methods:{
      ...mapActions({
        todosAdd : 'todosAdd',
        selectAll: 'selectAll'
      }),
      /*键盘输出enter事件*/
      recorded(){
        /*判断是否为空*/
        if (this.record === ''){
          return ;
        }
        /*插入对象中*/
        const data = {
          completed: false,
          record: this.record,
          show: true
        };
        /*插入todos数组中*/
        this.todosAdd(data);
        /*清空輸入框*/
        this.record = '';
      }
    }
  }
</script>

<style scoped>
  .header{
    text-align: center;
  }
  .query{
    height: 50px;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-items: center;
    align-items: center;
    border-bottom: 1px solid #bdcac7;
  }
  .query .checkbox{
    display: block;
    box-sizing: border-box;
    padding: 0px 5px;
    margin-left: 10px;
    height: 40px;
    text-decoration: none;
    outline: none;
    border: none;
    background-color: #fff;
  }
  .query button:hover{
    background-color: #bdcac7;
  }
  .query input{
    box-sizing: border-box;
    flex: 1;
    border: none;
    text-decoration: none;
    outline: none;
    height: 40px;
    line-height: 40px;
    font-size: 15px;
    padding-left: 10px;
  }
</style>
